package com.zzh.demo.agentDemo.visitor;

import com.zzh.demo.agentDemo.utils.ConfigUtils;

import java.util.LinkedList;

/**
 * description： <br>
 * createTime: 2024/2/29 14:00 <br>
 *
 * @author zzh
 */
public class TimeStatistics {
    public static ThreadLocal<LinkedList<Long>> t = ThreadLocal.withInitial(LinkedList::new);

    static long timeThreshold;

    static {
        timeThreshold = Long.parseLong(ConfigUtils.getStrProperties("timeThreshold", "2000"));
        System.out.println("timeThreshold = " + timeThreshold);
    }

    public static void start() {
        t.get().push(System.currentTimeMillis());
    }
    public static void end() {
        LinkedList<Long> longs = t.get();
        if(!longs.isEmpty()) {
            long startTime = longs.pop();
            long time = System.currentTimeMillis() - startTime;
            if(time >= timeThreshold) {
                System.out.println(Thread.currentThread().getStackTrace()[2] + " spend___: " + time);
            }
        }

    }
}
